#include <stdio.h>

#define LEN 25

int AddArr(int add[], int ad[], int result[]);

int SubArr(int add[], int ad[], int result[]);

int OutPut(int data[]);

int InputData(int data[], int len);

int CompareAbs(int a[], int b[]);

int Add(int add[], int ad[], int result[]);

int Sub(int add[], int ad[], int result[]);

int main()
{
    char op = 0;
    int add[LEN] = {0};
    int added[LEN] = {0};
    int result[LEN] = {0};
    int len = 5;
    int len_ed = 5;
    int err = -1;

    while(1){
        printf("length of first arg : \n");
        scanf("%d", &len);
        InputData(added, len);
        printf("length of second arg : \n");
        scanf("%d", &len_ed);

        InputData(add, len_ed);

        printf("sub or add ? (s for sub and a for add): \n");

        getchar();
        scanf("%c", &op);
        if('s' == op)
            err = Sub(added, add, result);
        else if('a' == op){
            err = Add(added, add, result);
        }

        if (err)
        {
            printf("overflow\n");
        }
        else
        {
            OutPut(result);
        }

        printf("continue ?(y for yes or others for no)\n");
        getchar();
        scanf("%c", &op);
        if('y' != op)
        {
            break;
        }
    }
    
    getchar();
    return 0;
}

int InputData(int data[], int len)
{
    scanf("%d", &data[LEN - len]);
    for (int i = LEN - len + 1; i < LEN; i++)
    {
        scanf(",%d", &data[i]);
    }
    return 0;
}

int SubArr(int add[], int ad[], int result[])
{
    int carry = 0;
    int x = 0;
    int y = 0;
    while (add[x] == 0)
    {
        x++;
    }
    while (ad[y] == 0)
    {
        y++;
    }
    if (add[x] < 0)
    {
        add[x] *= -1;
        // x = -1;
    }

    if (ad[y] < 0)
    {
        ad[y] *= -1;
        // y = -1;
    }
    for (int i = LEN - 1; i >= 0; i--)
    {
        result[i] = add[i] - ad[i] - carry;
        if (result[i] < 0)
        {
            carry = 1;
            result[i] += 10000;
        }
        else
        {
            carry = 0;
        }
    }
    return carry;
}

int AddArr(int add[], int ad[], int result[])
{
    int carry = 0;
    int x = 0;
    int y = 0;
    while (add[x] == 0)
    {
        x++;
    }
    while (ad[y] == 0)
    {
        y++;
    }
    if (add[x] < 0)
    {
        add[x] *= -1;
        // x = -1;
    }
    if (ad[y] < 0)
    {
        ad[y] *= -1;
        // y = -1;
    }

    for (int i = LEN - 1; i > -1; i--)
    {
        result[i] = (add[i] + ad[i] + carry) % 10000;
        carry = (add[i] + ad[i] + carry) / 10000;
        // add[i] = add[i] % 10000;
    }
    // OutPut(result);
    return carry;
}

int OutPut(int data[])
{
    int i = 0;
    while (data[i] == 0)
    {
        i++;
    }
    printf("%4d", data[i++]);
    while (i < LEN)
    {
        printf(",%04d", data[i]);
        i++;
    }
    printf("\n");

    return 0;
}

int CompareAbs(int a[], int b[])
{
    int i = 0;
    int j = 0;
    while (a[i] == 0)
    {
        i++;
    }
    while (b[j] == 0)
    {
        j++;
    }

    if (i < j)
    {
        return 1;
    }
    else if (i > j)
    {
        return -1;
    }
    else
    {
        if (a[i] < 0)
        {
            a[i] *= -1;
        }
        if (b[i] < 0)
        {
            b[i] *= -1;
        }
    }

    while (i < LEN)
    {
        if (a[i] < b[i])
            return -1;
        if (a[i] > b[i])
            return 1;
        i++;
    }
    return 0;
}

int Sub(int add[], int ad[], int result[])
{
    int err = 0;
    int x = 0;
    int y = 0;
    for (int i = 0; i < LEN; i++)
    {
        if (add[i] != 0)
        {
            x = add[i];
            break;
        }
    }

    for (int i = 0; i < LEN; i++)
    {
        if (ad[i] != 0)
        {
            y = ad[i];
            break;
        }
    }
    int dir = CompareAbs(add, ad);
    if (x * y > 0)
    {
        if (0 == dir)
        {
            return 0;
        }
        else if (dir < 0)
        {   // printf("bug here\n");
            err = SubArr(ad, add, result);
            if (x > 0)
            {
                for (int i = 0; i < LEN; i++)
                {
                    if (result[i] != 0)
                    {
                        result[i] *= -1;
                        break;
                    }
                }
            }
        }
        else
        {
            // printf("bug here\n");
            err = SubArr(add, ad, result);
            if (x < 0)
            {
                for (int i = 0; i < LEN; i++)
                {
                    if (result[i] != 0)
                    {
                        result[i] *= -1;
                        break;
                    }
                }
            }
        }
    }
    else
    {
        err = AddArr(add, ad, result);
        if (x < 0)
        {
            for (int i = 0; i < LEN; i++)
            {
                if (result[i] != 0)
                {
                    result[i] *= -1;
                    break;
                }
            }
        }
    }
    return err;
}

int Add(int add[], int ad[], int result[])
{
    int err = 0;
    int x = 0;
    int y = 0;
    for (int i = 0; i < LEN; i++)
    {
        if (add[i] != 0)
        {
            x = add[i];
            // x = add[i] > 0 ? add[i] : -add[i];
            break;
        }
    }

    for (int i = 0; i < LEN; i++)
    {
        if (ad[i] != 0)
        {
            y = ad[i];
            // y = ad[i] > 0 ? ad[i] : -ad[i];
            break;
        }
    }
    int dir = CompareAbs(add, ad);

    if (x * y > 0)
    {
        // printf("tong hao\n");
        err = AddArr(add, ad, result);
        if (x < 0)
        {
            for (int i = 0; i < LEN; i++)
            {
                if (result[i] != 0)
                {
                    result[i] *= -1;
                    break;
                }
            }
        }
    }
    else
    {
        if (!dir)
        {
            for (int i = 0; i < LEN; i++)
            {
                result[i] = 0;
            }
        }
        else if (0 > dir)
        {
            err = SubArr(ad, add, result);
            if (x > y)
            {
                for (int i = 0; i < LEN; i++)
                {
                    if (result[i] != 0)
                    {
                        result[i] *= -1;
                        break;
                    }
                }
            }
        }
        else
        {
            // printf("here\n");
            err = SubArr(add, ad, result);
            if (x < y)
            {
                for (int i = 0; i < LEN; i++)
                {
                    if (result[i] != 0)
                    {
                        result[i] *= -1;
                        break;
                    }
                }
            }
        }
    }
    return err;
}